var searchTable;
var menuLength=getLength("config_page_size");
var btnModel = '    \
	{{#each func}}\
    <button type="button" class="{{this.type}} btn-sm" onclick="{{this.fn}}">{{this.name}}</button>\
    {{/each}}';
	var template = Handlebars.compile(btnModel);
	var staffdataTableConfig={
			"ordering" : false,// 排序
			"serverSide" : true,// 开启服务器模式
			"scrollX" : true,// 横向滚动
			ajax : {
				"type" : "POST",
				"url" : '../sync/staffList',//请求路径
				"contentType" : 'application/x-www-form-urlencoded; charset=UTF-8',
				"dataType":"json",
				"data" : function(d){// 查询参数
	            	d.staffCode = $('#codeObj').val();
	            	d.staffName = $('#nameObj').val();
	            	d.empCode = $('#empObj').val();
	            	d.loginName = $('#emailObj').val();
	            	return d;
	            }
			},
			columns : [// 对应列
	           	{"data" : null,		render : function(data, type, full, meta) {
	           		var v=JSON.stringify(data);
	           		return '<input type="checkbox" value=' + v+ ' name="checkbox" />';
				}}, 
	           	{"data" : "staffCode",render : $.fn.dataTable.render.ellipsis(22, true)}, 
				{"data" : "staffName",render : $.fn.dataTable.render.ellipsis(22, true)},
				{"data" : "empCode",render : $.fn.dataTable.render.ellipsis(22, true)}, 
				{"data" : "sex",render : $.fn.dataTable.render.ellipsis(22, true)},
				{"data" : "email",render : $.fn.dataTable.render.ellipsis(22, true)}, 
				{"data" : "staffStatus",render :function(data, type, full, meta) {
					if (data == 0) {
						return "在职";
					} else {
						return "离职";
					}
				}},
				{"data" : "siteType",render : function(data, type, full, meta) {
					if (data == 1) {
						return "主岗";
					} else {
						return "兼职";
					}
				}},
				{"data" : "siteDeptName",render : $.fn.dataTable.render.ellipsis(22, true)},
				{"data" : "siteCreateDate",render : $.fn.dataTable.render.ellipsis(22, true)}
	           ],
			"columnDefs" : [ {// 所有列默认值
				"targets" : "_all",
				"defaultContent" : ''
				} ],
				"dom" : 'rt<"floatl mt5"l><"floatl mt5"i><"floatr mt5"p><"clear">' ,//生成样式
				"lengthMenu": [
				        menuLength,
				        menuLength
				    ]
	};
	var orgdataTableConfig={
			"ordering" : false,// 排序
			"serverSide" : true,// 开启服务器模式
			"scrollX" : true,// 横向滚动
			ajax : {
				"type" : "POST",
				"url" : '../sync/orgList',//请求路径
				"contentType" : 'application/x-www-form-urlencoded; charset=UTF-8',
				"dataType":"json",
				"data" : function(d){// 查询参数
	            	d.mdmOrgCode = $('#mdmOrgCodeObj').val();
	            	d.hrOrgCode = $('#hrOrgCodeObj').val();
	            	d.orgName = $('#orgNameObj').val();
	            	d.cuncOrgClass = $('#cuncOrgClassObj').val();
	            	d.cuncOrgScope = $('#cuncOrgScopeObj').val();
	            	return d;
	            }
			},
			columns : [// 对应列
	           	{"data" : null,		render : function(data, type, full, meta) {
	           		var v=JSON.stringify(data);
	           		return '<input type="checkbox" value=' + v+ ' name="checkbox2" />';
				}}, 
	           	{"data" : "mdmOrgCode",render : $.fn.dataTable.render.ellipsis(22, true)}, 
				{"data" : "hrOrgCode",render : $.fn.dataTable.render.ellipsis(22, true)},
				{"data" : "orgName",render : $.fn.dataTable.render.ellipsis(22, true)}, 
				{"data" : "orgNameDisplay",render : $.fn.dataTable.render.ellipsis(22, true)}, 
				{"data" : "cuncOrgClass",render :
 					function(data, type, full, meta) {
						var typeName;
						switch (data) {
						case "ORG":
							typeName="组织";
							break;
						case "DEPT":
							typeName="部门";
							break;
						case "VORG":
							typeName="虚拟组织";
							break;
						case "S_CHNL":
							typeName="社会渠道";
							break;
						case "O_CHNL":
							typeName="自有渠道";
							break;
						}
						return typeName;
					}
				},
				{"data" : "cuncOrgScope",render : 
					function(data, type, full, meta) {
							var scopeName;
							switch (data) {
							case "1":
								scopeName="正式组织";
								break;
							case "2":
								scopeName="SP/CP合作伙伴组织";
								break;
							case "3":
								scopeName="供应商组织";
								break;
							case "4":
								scopeName="渠道合作伙伴组织";
								break;
							case "5":
								scopeName="临时组织";
								break;
							}
							return scopeName;
					}
				}, 
				{"data" : "deletedFlag",render :function(data, type, full, meta) {
					if (data == 0) {
						return "有效";
					} else {
						return "失效";
					}
				}}
	           ],
			"columnDefs" : [ {// 所有列默认值
				"targets" : "_all",
				"defaultContent" : ''
				} ],
				"dom" : 'rt<"floatl mt5"l><"floatl mt5"i><"floatr mt5"p><"clear">' ,//生成样式
				"lengthMenu": [
				        menuLength,
				        menuLength
				    ]
	};
	//前台校验参数
	var checkValidator = {
			message: 'This value is not valid',
	        feedbackIcons: {
	            valid: 'glyphicon glyphicon-ok',
	            invalid: 'glyphicon glyphicon-remove',
	            validating: 'glyphicon glyphicon-refresh'
	        },
	        submitHandler: function(validator, form, submitButton) {
	        	var id=submitButton.attr("id");
	    		if(id=="updateBtn"){
	    			update();
	    		}
	        }

	};
	//查询，重新载入表格
	function reloadStaff() {
		$("#staffTable").dataTable().fnDraw(false);
	}
	//查询，重新载入表格
	function reloadOrg() {
		$("#orgTable").dataTable().fnDraw(false);
	}
	//全反选
	function selectAllorNot(){
		if($("#checkAll").is(':checked')){
			$("input[name='checkbox']").prop("checked","checked"); 
		}else{
			$("input[name='checkbox']").removeProp("checked");
		}
	}
	//获取选中记录
	function getStaffCheck(){
		var staffes=new Array();
		$('input[name="checkbox"]:checked').each(function(){ 
			staffes.push(JSON.parse($(this).val())); 
			});
		return staffes;
	}
	//获取选中记录
	function getOrgCheck(){
		var staffes=new Array();
		$('input[name="checkbox2"]:checked').each(function(){ 
			staffes.push(JSON.parse($(this).val())); 
			});
		return staffes;
	}
	function syncStaff(){
		var staffes=new Array();
		staffes=getStaffCheck();
		if (staffes.length == 0) {
			alertModel("请至少选中一条记录");
			return;
		}
		$.ajax({
			url:"../sync/syncStaff",
			data:JSON.stringify(staffes), 
			contentType:"application/json",  
			type : "post",
			success : function(data){
				alertModel("同步成功");
			}
		})
	}
	function syncOrg(){
		var orgs=new Array();
		orgs=getOrgCheck();
		if (orgs.length == 0) {
			alertModel("请至少选中一条记录");
			return;
		}
		$.ajax({
			url:"../sync/syncOrg",
			data:JSON.stringify(orgs), 
			contentType:"application/json",  
			type : "post",
			success : function(data){
				alertModel("同步成功");
			}
		})
	}
	
	//重置校验
	function resetValidator(){
		$("#valObj").empty();
		if($('#configForm').data('bootstrapValidator')){
			$('#configForm').data('bootstrapValidator').resetForm(false);
		}
	}
	$(function() {
		$('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
			  if("other" === e.target.id){
				  reloadOrg();
			  }else{
				  reloadStaff();
			  }
			});
		 $("#staffTable").DataTable(staffdataTableConfig);
		 $('#orgAnchor').one("click",function (){
			 $("#orgTable").DataTable(orgdataTableConfig);
			 reloadOrg();
		 })
	})
